#ifndef MAX_STACKWIDGET_H
#define MAX_STACKWIDGET_H

#include <QStackedWidget>
#include <QPropertyAnimation>
#include <QEasingCurve>
#include <QPainter>
#include <QSequentialAnimationGroup>
#include <QParallelAnimationGroup>
#include <QDebug>
class max_stackwidget : public QStackedWidget {
    Q_OBJECT
public:
    enum AnimationType {
        SlideHorizontal, // 水平滑动（默认）
        SlideVertical,   // 垂直滑动
        Fade             // 淡入淡出
    };
    Q_ENUM(AnimationType)
    max_stackwidget();
    explicit max_stackwidget(QWidget *parent = nullptr)
        : QStackedWidget(parent), m_animationType(SlideHorizontal), m_duration(400) {}

    // 设置动画类型
    void setAnimationType(AnimationType type) {
        m_animationType = type;
    }

    // 设置动画时长（毫秒）
    void setAnimationDuration(int duration) {
        m_duration = duration;
    }
    void changeCurrentIndex(int index)  {
        // qDebug() << currentIndex();
        if (currentIndex() == index || index >= count() || index < 0) return;

        // 如果正在动画中，先完成当前动画
        if (m_currentAnimation && m_currentAnimation->state() == QAbstractAnimation::Running) {
            m_currentAnimation->stop();
            widget(currentIndex())->hide();
        }

        // 获取新旧页面
        QWidget *oldWidget = widget(currentIndex());
        QWidget *newWidget = widget(index);
        // 强制更新窗口
        // update();
        // 执行动画
        switch (m_animationType) {
        case SlideHorizontal: slideAnimation(oldWidget, newWidget); break;
        case SlideVertical: slideAnimation(oldWidget, newWidget, false); break;
        case Fade: fadeAnimation(oldWidget, newWidget); break;
        }

        //QStackedWidget::setCurrentIndex(index); // 更新当前索引
    }
    void changeCurrentWidget(QWidget *newWidget) {
        int newIndex = indexOf(newWidget);
        if (newIndex == -1) return; // 目标页面不在栈中

        // qDebug() << currentIndex();
        if (currentIndex() == newIndex) return;

        // 如果正在动画中，先完成当前动画
        if (m_currentAnimation && m_currentAnimation->state() == QAbstractAnimation::Running) {
            m_currentAnimation->stop();
            widget(currentIndex())->hide();
        }

        // 获取新旧页面
        QWidget *oldWidget = widget(currentIndex());

        // 强制更新窗口
        // update();

        // 执行动画
        switch (m_animationType) {
        case SlideHorizontal: slideAnimation(oldWidget, newWidget); break;
        case SlideVertical: slideAnimation(oldWidget, newWidget, false); break;
        case Fade: fadeAnimation(oldWidget, newWidget); break;
        }

        //QStackedWidget::setCurrentIndex(newIndex); // 更新当前索引
    }
private:
    AnimationType m_animationType;
    int m_duration;
    QAbstractAnimation *m_currentAnimation = nullptr;

    // 滑动动画
    void slideAnimation(QWidget *oldWidget, QWidget *newWidget, bool horizontal = true) {
        const int offset = horizontal ? width() : height();
        const bool isRTL = layoutDirection() == Qt::RightToLeft;

        // 初始化新页面位置
        newWidget->setGeometry(0, 0, width(), height());
        newWidget->show();
        newWidget->raise();

        // 根据方向计算位置
        QPoint newStart, oldEnd;
        if (horizontal) {
            newStart = isRTL ? QPoint(-offset, 0) : QPoint(offset, 0);
            oldEnd = isRTL ? QPoint(offset, 0) : QPoint(-offset, 0);
        } else {
            newStart = QPoint(0, offset);
            oldEnd = QPoint(0, -offset);
        }

        // 创建并行动画组
        QParallelAnimationGroup *group = new QParallelAnimationGroup(this);

        // 旧页面动画
        QPropertyAnimation *oldAnim = new QPropertyAnimation(oldWidget, "pos");
        oldAnim->setDuration(m_duration);
        oldAnim->setStartValue(oldWidget->pos());
        oldAnim->setEndValue(oldEnd);
        oldAnim->setEasingCurve(QEasingCurve::OutCubic);

        // 新页面动画
        QPropertyAnimation *newAnim = new QPropertyAnimation(newWidget, "pos");
        newAnim->setDuration(m_duration);
        newAnim->setStartValue(newStart);
        newAnim->setEndValue(QPoint(0, 0));
        newAnim->setEasingCurve(QEasingCurve::OutCubic);

        group->addAnimation(oldAnim);
        group->addAnimation(newAnim);
        m_currentAnimation = group;

        // 动画结束时隐藏旧页面
        connect(group, &QAbstractAnimation::finished, [oldWidget]() {
            oldWidget->hide();
        });

        group->start();
    }

    // 淡入淡出动画
    void fadeAnimation(QWidget *oldWidget, QWidget *newWidget) {
        newWidget->setGeometry(0, 0, width(), height());
        newWidget->show();
        newWidget->raise();

        // 创建串行动画组
        QSequentialAnimationGroup *group = new QSequentialAnimationGroup(this);

        // 淡出旧页面
        QPropertyAnimation *fadeOut = new QPropertyAnimation(oldWidget, "windowOpacity");
        fadeOut->setDuration(m_duration/2);
        fadeOut->setStartValue(1.0);
        fadeOut->setEndValue(0.0);

        // 淡入新页面
        QPropertyAnimation *fadeIn = new QPropertyAnimation(newWidget, "windowOpacity");
        fadeIn->setDuration(m_duration/2);
        fadeIn->setStartValue(0.0);
        fadeIn->setEndValue(1.0);

        group->addAnimation(fadeOut);
        group->addAnimation(fadeIn);
        m_currentAnimation = group;

        // 动画结束时隐藏旧页面
        connect(group, &QAbstractAnimation::finished, [oldWidget]() {
            oldWidget->hide();
            oldWidget->setWindowOpacity(1.0); // 重置透明度
        });

        group->start();
    }
};

#endif // MAX_STACKWIDGET_H
